#include ".\terrain.h"

namespace RBS
{

	Terrain::Terrain(void)
	{
	}						  

	Terrain::~Terrain(void)
	{
	}

	HRESULT Terrain::Init(LPDIRECT3DDEVICE9 pDevice)
	{
		VKObject::Init(pDevice);

//		TERRAIN_VERTEX vertices[4 * 25];
//		TERRAIN_VERTEX *pCurrentVertex = &vertices[0];
//
//		float fWidth = 0.1f;
//		float fHeight = 0.1f;
//
//		float fx = -1.0f;
//		float fz = 0.0f;
//
//		for(int x = 0 ; x < 5 ; ++x)
//		{
//			for(int y = 0 ; y < 5 ; ++y)
//			{
//				pCurrentVertex->x = fx;
//				pCurrentVertex->y = 0.0f;
//				pCurrentVertex->z = fz + fHeight;
//				++pCurrentVertex;
//
//				pCurrentVertex->x = fx;
//				pCurrentVertex->y = 0.0f;
//				pCurrentVertex->z = fz;
//				++pCurrentVertex;
//
//				pCurrentVertex->x = fx + fWidth;
//				pCurrentVertex->y = 0.0f;
//				pCurrentVertex->z = fz + fHeight;
//				++pCurrentVertex;
//
//				pCurrentVertex->x = fx + fWidth;
//				pCurrentVertex->y = 0.0f;
//				pCurrentVertex->z = fz;
//				++pCurrentVertex;
//
//				fx += fWidth;
//			}
//
//			fx = x * fWidth;
//			fz += fHeight;
//		}

		TERRAIN_VERTEX vertices[3 * 4] = 
		{
			{ 0,0,1 , 0x00ffffff } , 
			{ 0,0,0 , 0x00ffffff } , 
			{ 1,0,1 , 0x00ffffff } , 
			{ 1,0,0 , 0x00ffffff }

		};

		VOID* pVertices;

		HRESULT hr = m_pDevice->CreateVertexBuffer( sizeof(vertices) , 0, FVF_TERRAIN_VERTEX, D3DPOOL_DEFAULT, &m_pVBuff, NULL );
		if( FAILED( hr ) )		throw hr;

		hr = m_pVBuff->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 );
		if( FAILED( hr ) )		throw hr;

		memcpy( pVertices, vertices, sizeof(vertices) );
		m_pVBuff->Unlock();

		return S_OK;
	}

	HRESULT Terrain::Render()
	{
		m_pDevice->SetRenderState(D3DRS_LIGHTING , TRUE);
		m_pDevice->SetStreamSource( 0, m_pVBuff, 0, sizeof(TERRAIN_VERTEX) );
		m_pDevice->SetFVF( FVF_TERRAIN_VERTEX );
		m_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );


		return S_OK;
	}


}